<?php

/**
 *
 * @file   Menu.php
 * @date   2016-8-30 11:46:22
 * @author Zhenxun Du<5552123@qq.com>
 * @version    SVN:$Id:$
 */

namespace app\admin\controller;

use think\Db;
use think\Loader;

class IdentifyController extends CommonController
{

    public function index()
    {
        $res = db('menu')->order('listorder asc')->select();

        $lists = nodeTree($res);
        $this->assign('lists', $lists);
        return $this->fetch();
    }

    public function getIdentifyList()
    {

        $start = input('start');
        $length = input('stalengthrt');
        $draw = input('draw');
        $search = input('post.search/a');
        $search = $search['value'];
        //$ser = $search?['','like','%'.$search.'%']:[];

        $data = Db::name("figure")
            ->alias('a')
            ->join('fd_user u', 'u.id=a.user_id')
            ->where('fig_name', 'like', "%$search%")
            ->limit($start * $length, $length)->field('a.id,a.user_id,a.fig_name,a.fig_phone,a.fig_job,a.fig_company,a.fig_achievement,a.fig_introduction,a.hand_card,a.company_image,a.status,a.created_at,a.updated_at')->select();
        $count = Db::name("figure")
            ->alias('a')
            ->join('fd_user u', 'u.id=a.user_id')
            ->where('fig_name', 'like', "%$search%")
            ->count();
        if ($count) {
            foreach ($data as &$v) {
                $v['company_image'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$v['company_image'];
                $v['hand_card'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$v['hand_card'];
                $v['options'] = "<div class='hidden-sm hidden-xs action-buttons'>
									<a class='green' href='/admin/Identify/info?id=" . $v['id'] . "'>
										<i class='ace-icon fa fa-pencil bigger-130'></i>
									</a>
								</div>";
            }
        }
        $temp = [
            'draw' => $draw,
            'data' => $data,
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
        ];
        echo json_encode($temp);
    }

    /*
     * 查看
     */

    public function info()
    {
        $id = input('id');
        if ($id) {
            //当前用户信息
            $info = Db::name("figure")
                ->alias('a')
                ->join('fd_user u', 'u.id=a.user_id')
                ->join('user_extend b', 'u.phone=b.phone')
                ->field('a.id,a.user_id,a.fig_name,a.fig_phone,a.fig_job,a.fig_company,a.fig_achievement,a.fig_introduction,a.hand_card,a.company_image,a.status,a.created_at,a.updated_at')
                ->where(['a.id'=>$id])
                ->select();
            $info = $info[0];
            $info['hand_card'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$info['hand_card'];
            $info['company_image'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$info['company_image'];
            $this->assign('info', $info);
        }
        //下拉菜单
        //$this->assign('selectMenu', Loader::model('Menu')->selectMenu());
        return $this->fetch();
    }

    /*
     * 添加
     */

    public function add()
    {
        $data = input();
        if ($data['parentid'] == null) {
            $data['parentid'] = 0;
        }
        $res = model('menu')->allowField(true)->save($data);
        if ($res) {
            $this->success('操作成功', url('index'));
        } else {
            $this->error('操作失败');
        }
    }

    /*
     * 修改
     */

    /*public function edit()
    {
        $data['id'] = input('post.id');
        $data['status'] = input('post.status');
        $updated_at = date('Y-m-d H:i:s',time());
        $status = $data['status'];
        $res = db('figure')->where(['id' => $data['id']])->update(['updated_at' => $updated_at,'status'=>$status]);
//        $res = model('fd_figure')->allowField(true)->save($where, ['id' => $data['id']]);
        if ($res) {
            //同时更新user表中is_figure字段
            $res = db('figure')->field('user_id')->where(['id' => $data['id']])->find();
            if($res){
                $res = db('user')->where(['id' => $res['user_id']])->update(['is_figure'=>1]);
                if($res){
                    $this->success('操作成功', url('index'));
                }else{
                    $this->error('操作失败');
                }
            }else{
                $this->error('操作失败');
            }
        } else {
            $this->error('操作失败');
        }
    }*/
    /***
     * 影人认证审核
     */
    public function edit()
    {
        $id = input('post.id');
        $status = input('post.status');
        $updated_at = date('Y-m-d H:i:s',time());
        $res = Db::name('figure')->field('user_id')->where(['id' => $id])->find();
        if($res){
            //开启事务
            Db::startTrans();
            try {
                $figureRes = Db::name('figure')->where(['id' => $id])->update([
                    'updated_at' => $updated_at,
                    'status'=>$status
                ]);
                if(!$figureRes){
                    throw new \Exception('更新表figure失败');
                }
                $userRes = Db::name('user')->where(['id' => $res['user_id']])->update(['is_figure'=>1]);
                if(!$userRes){
                    throw new \Exception('更新表user失败');
                }
                Db::commit();
                header('location:/admin/Identify/index');
            } catch (\Exception $e) {
                Db::rollback();
                header('location:/admin/index/index');
            }
        }
    }
    /*
     * 删除
     */

    public function del()
    {
        $id = input('id');
        $res = db('menu')->where(['id' => $id])->delete();
        if ($res) {
            $this->success('操作成功', url('index'));
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 排序
     */
    public function setListorder()
    {

        if ($_POST['listorder']) {
            $listorder = $_POST['listorder'];
            foreach ($listorder as $k => $v) {
                $data = array();
                $data['listorder'] = $v;
                $data['updatetime'] = time();
                $res = db('menu')->where(['id' => $k])->update($data);
            }
            if ($res) {
                $this->success('操作成功', url('index'));
            } else {
                $this->error('操作失败');
            }
        }
    }

}
